package com.wx.controller.api;

import com.wx.common.WrapMapper;
import com.wx.common.Wrapper;
import com.wx.constants.Constants;
import com.wx.domain.vo.NurseVo;
import com.wx.entity.SysPatient;
import com.wx.entity.SysRange;
import com.wx.holder.WxUserHolder;
import com.wx.service.SysPatientService;
import com.wx.service.SysRangeService;
import com.wx.service.ValidateCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>Title:      ApiPatientController. </p>
 * <p>Description  </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company:    医百科技有限公司 </p>
 *
 * @author         yangwensheng@100doc.com.cn
 * @since      2021-03-03 11:36:07
 */
@RestController
@RequestMapping(value = "/api/sysPatient", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(tags = "患者管理(API)", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ApiPatientController {

    @Resource
    private SysPatientService sysPatientService;

    @Resource
    private ValidateCodeService validateCodeService;

    @Resource
    private SysRangeService sysRangeService;

    @Resource
    private WxUserHolder wxUserHolder;


    @GetMapping("/getPatientInfoByOpenId")
    @ApiOperation(httpMethod = "GET", value = "根据openId查询患者信息")
    public Wrapper<?> getPatientInfoByOpenId() {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        SysPatient result = sysPatientService.getPatientInfoByOpenId(userInfo.getOpenid());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }

    @PostMapping("/generateSmsCode/{mobile}")
    @ApiOperation(value = "生成短信验证码")
    public Wrapper<?> generateSmsCode(@PathVariable("mobile") String mobile) {
        validateCodeService.generateSmsCode(mobile);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE);
    }


    @PostMapping("/checkValidateCode/{mobile}/{validateCode}")
    @ApiOperation(httpMethod = "POST", value = "校验患者的短信验证码是否正确")
    public Wrapper<?> checkValidateCode(@PathVariable("mobile") String mobile, @PathVariable("validateCode") String validateCode) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        SysPatient result = sysPatientService.checkValidateCode(userInfo.getOpenid(),mobile,validateCode);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }


    @PostMapping("/onlineApplication")
    @ApiOperation(httpMethod = "POST", value = "患者线上申请入组")
    public Wrapper<String> onlineApplication(@ApiParam(name = "sysPatient", value = "sysPatient") @RequestBody @Validated SysPatient sysPatient) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        sysPatient.setOpenId(userInfo.getOpenid());
        String result = sysPatientService.onlineApplication(sysPatient);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }

    @PostMapping("/updatePatient")
    @ApiOperation(httpMethod = "POST", value = "更新患者的认证状态")
    public Wrapper<?> updatePatient(@ApiParam(name = "sysPatient", value = "sysPatient") @RequestBody SysPatient sysPatient) {
        int result = sysPatientService.updateStatus(sysPatient);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }


    @PostMapping("/updateAgreement/{id}")
    @ApiOperation(httpMethod = "POST", value = "更新协议签署状态")
    public Wrapper<Integer> updateAgreement(@PathVariable("id") String id) throws Exception {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        int result = sysPatientService.updateAgreement(id, userInfo.getOpenid(), userInfo.getNickname());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }


    @PostMapping("/listRange")
    @ApiOperation(httpMethod = "POST", value = "下拉列表查询所有范围")
    public Wrapper<List<SysRange>> listRange() {
        SysRange query = new SysRange();
        query.setDelFlag(Constants.DELETE_NO);
        List<SysRange> list = sysRangeService.select(query);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, list);
    }


    @GetMapping("/queryNurseByPatientOfOpenId")
    @ApiOperation(httpMethod = "GET", value = "根据患者的openId查询所属的护士信息")
    public Wrapper<NurseVo> queryNurseByPatientOfOpenId() {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        NurseVo nurse = sysPatientService.queryNurseByPatientOfOpenId(userInfo.getOpenid());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,nurse);
    }


    @GetMapping("/getPatientInfoById/{patientId}")
    @ApiOperation(httpMethod = "GET", value = "根据id查询患者信息")
    public Wrapper<?> getPatientInfoById(@PathVariable("patientId") String patientId) {
        SysPatient result = sysPatientService.getPatientInfoById(patientId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }

}